﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class UnitNetizen : MonoBehaviour
{
    public float speed = 1f;
    public Transform transparent;
    public Animation anim;
    public Queue<KeyValuePair<Item, List<UnitPlot>>> paths = new Queue<KeyValuePair<Item, List<UnitPlot>>>();
    private int currentWaypointIndex = 0;
    private List<UnitPlot> currentPath = new List<UnitPlot>();
    private Item task;
    private bool isDoingTask = false;
    private List<GameObject> chuans = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (!isDoingTask && paths.Count > 0)
        {
            var kv = paths.Dequeue();
            task = kv.Key;
            currentPath = kv.Value;
            currentWaypointIndex = 0;
        }
        if (currentWaypointIndex < currentPath.Count)
        {
            Transform currentWaypoint = currentPath[currentWaypointIndex].transform;
            Vector3 direction = currentWaypoint.position - transform.position;
            if (Vector3.Angle(transform.forward, direction) > 45)
            {
                transform.rotation = Quaternion.LookRotation(direction);
            }
            if (chuans.Count > 0)
            {
                anim.Play("WalkCarry");
            }
            else
                anim.Play("Walk");
            transform.Translate(direction.normalized * speed * Time.deltaTime, Space.World);
            if (Vector3.Distance(transform.position, currentWaypoint.position) < 0.15f)
            {
                currentWaypointIndex++;
                if (currentWaypointIndex >= currentPath.Count)
                {
                    anim.CrossFade("Idle", 0.2f);
                    DoTask();
                }
            }
        }
    }

    void CheckDeskNeed()
    {

    }

    public void DoTask()
    {
        isDoingTask = true;
        switch (task.type)
        {
            case ItemType.TABLE:
                StartCoroutine(TableTask());
                break;
            case ItemType.DESK:
                transform.LookAt(task.gameObject.transform.position);
                StartCoroutine(DeskTask());
                break;
                //for (int i = 0; i < chuans.Count; i++)
                //    Destroy(chuans[i]);
                //chuans.Clear();
                //isDoingTask = false;
                //break;
        }
    }

    IEnumerator DeskTask()
    {
        yield return null;
        ((Desk)task).TaskEnqueue(chuans.ToArray());
        //for (int j = 0; j < ((Desk)task).dicNeed.Keys.Count; j++)
        //{
        //    print(((Desk)task).dicNeed.Keys.ToArray()[j]);
        //    print(((Desk)task).dicNeed.Keys.ToArray()[j]);
        //}
        //for (int i = 0; i < chuans.Count; i++)
        //{
        //    print(chuans[i].transform.GetChild(0).name);
        //}
        //int need = 2 - chuans.Count;
        //if (need > 0)
        //{
        //    ((Desk)task).TaskEnqueue(objs);
        //    for (int i = 0; i < objs.Length; i++)
        //    {
        //        chuans.Add(objs[i]);
        //    }
        //    for (int i = 0; i < chuans.Count; i++)
        //    {
        //        chuans[i].transform.SetParent(transparent, false);
        //        chuans[i].transform.localRotation = Quaternion.Euler(new Vector3(90, 270, 0));
        //        chuans[i].transform.localPosition = new Vector3(0.5f, 0.4f, 0);
        //    }
        //}
        isDoingTask = false;
    }

    IEnumerator TableTask()
    {
        yield return null;
        int need = 2 - chuans.Count;
        if (need > 0)
        {
            var objs = ((Table)task).TaskDequeue(need);
            for (int i = 0; i < objs.Length; i++)
            {
                chuans.Add(objs[i]);
            }
            for (int i = 0; i < chuans.Count; i++)
            {
                chuans[i].transform.SetParent(transparent, false);
                chuans[i].transform.localRotation = Quaternion.Euler(new Vector3(90, 270, 0));
                chuans[i].transform.localPosition = new Vector3(0.5f, 0.4f, 0);
            }
        }
        isDoingTask = false;
    }
}